<?php
class TeacherLogDao {
	private $table = 'teacher_log';
	public function add($data){
		ClassManager::getSql()->insert($this->table, $data);
	}
	private function searchWhere($where){
		$where1 = array();
		foreach ($where as  $k=>$v){
			if (!empty($v)){
				$where1[$k] =  $v;
			}
		}
		unset($where);
		return $where1;
	}
	public function searchData($where=array()){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->onedata1($this->table,'*'.$where);
	}
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	public function searchNumber($where= array()){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	public function searchBookCourseLogList($where=array(),$order='',$limit=0){
		$where = $this->searchMutilWhere($where);
		$sql = " select bc.book_no,bc.class_status,bc.teacher_status,bc.english_name,bc.id,bc.user_status,bc.book_date,bc.book_time,bc.complete_time,tl.type,tl.money,tl.integral,tl.add_time "  ;
		$sql.= " from eo_teacher_log tl left join  eo_book_course bc on tl.target_id=bc.id ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$sql.= " order by bc.id desc ";
		return ClassManager::getSql()->dataMutillist('bookCourse list and teacher_log ',$sql,$limit);
	}
	public function searchBookCourseLogNumber($where= array()){
		$where = $this->searchMutilWhere($where);
		$sql = "select count(1) as cnt  from (select 1 "  ;
		$sql.= " from eo_teacher_log tl left join  eo_book_course bc on tl.target_id=bc.id ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$sql.= " ) c  ";
		$tmp =  ClassManager::getSql()->dataMutillist('bookCourse num and teacher_log ',$sql,$limit);

		return $tmp[0]['cnt'];
	}


	public function searchGroupList($where=array(),$order='',$limit=0){
		$where = $this->searchMutilWhere($where);
		$sql = " select tl.teacher_id,t.serial_number,t.real_education_experience,t.name,sum(money) as money,sum(integral) integral "  ;
		$sql.= " from eo_teacher_log tl left join  eo_book_course bc on tl.target_id=bc.id ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$sql.= " group by tl.teacher_id ";
		return ClassManager::getSql()->dataMutillist('grouplist and teacher_log ',$sql,$limit);
	}
	
	public function searchGroupNumber($where=array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select count(1) cnt from (select 1  ";
		$sql.= " from eo_teacher_log tl left join  eo_book_course bc on tl.target_id=bc.id ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$sql.= " group by tl.teacher_id ) log  ";
		$tmp =  ClassManager::getSql()->dataMutillist('group and teacher_log ',$sql,$limit);
		return $tmp[0]['cnt'];
	}
	
	public function searchMutilWhere($where){
		$where1 = '';

		if(isset($where['start_time'])&&$where['start_time']>0){
			$where1 .=" and  bc.add_time >='".$where['start_time']."'";
		}
		
		if(isset($where['over_time'])&&$where['over_time']>0){
			$where['over_time'] = $where['over_time']+86399;
			$where1 .=" and  bc.add_time <='".$where['over_time']."'";
		}
		
		if(isset($where['name'])&&!empty($where['name'])){
			$where1 .=" and  t.name like '%".$where['name']."%'";
		}
		
		if(isset($where['serial_number'])&&!empty($where['serial_number'])){
			$where1 .=" and  t.serial_number like '%".$where['serial_number']."%'";
		}
		
		if(isset($where['teacher_id'])&&!empty($where['teacher_id'])){
			$where1 .=" and  tl.teacher_id ='".$where['teacher_id']."'";
		}
		
		if(isset($where['real_education_experience'])&&$where['real_education_experience']>0){
			$where1 .=" and  t.real_education_experience='".$where['real_education_experience']."'";
		}
		
		if(isset($where['book_no'])&&!empty($where['book_no'])){
			$where1 .=" and  bc.book_no like '%".$where['book_no']."%'";
		}
		
		return $where1;
	}
	
	public function getSumGroupInfo($where = array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select sum(money) as money,sum(integral) integral "  ;
		$sql.= " from eo_teacher_log tl left join  eo_book_course bc on tl.target_id=bc.id ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$tmp = ClassManager::getSql()->dataMutillist('grouplist_summoney and teacher_log ',$sql);
		return $tmp;
	}
		
	
	public function getSumMoney($where = array()){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"sum(money)  ",$where);
		return $tmp[0];
	}


	public function getTeacherMxSumInfo($where = array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select sum(money) as money,sum(integral) integral,tl.teacher_id teacher_id,t.base_integral,t.real_education_experience "  ;
		$sql.= " from eo_teacher_log tl ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$sql.= " group by tl.teacher_id ";		
		$tmp = ClassManager::getSql()->dataMutillist('teacher mx info and teacher_log ',$sql);	
		return $tmp;
	}	
	
	public function getTeacherSumInfo($where = array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select sum(money) as money,sum(integral) integral "  ;
		$sql.= " from eo_teacher_log tl left join  eo_book_course bc on tl.target_id=bc.id ";
		$sql.= " left join  eo_teacher t  on tl.teacher_id=t.id ";
		$sql.= " where 1=1 ";
		$sql.= $where;
		$tmp = ClassManager::getSql()->dataMutillist('teachersum info and teacher_log ',$sql);	
		return $tmp[0] ;
	}
	
	public function deleteByBookCourseId($id){
		ClassManager::getSql()->delete($this->table,"where target_id = '".$id."'");
	}
}
?>